package com.campus_information_portal.controller;
import com.campus_information_portal.entity.Course;
import com.campus_information_portal.service.AcademyService;
import com.campus_information_portal.service.CourseService;
import com.campus_information_portal.util.Message;
import com.campus_information_portal.util.Msg;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/course")
@ResponseBody
@CrossOrigin(origins = "*")
public class CourseController {
    @Autowired
    private CourseService courseService;
    @Autowired
    private AcademyService academyService;
    /**
     * @Description 增加课程Action层
     * @author 20155878 鄢前州
     * @methodName addCourseAction
     * @returnType Msg
     * @create 21:51 2018/5/13
     **/
    @RequestMapping(value = "/addCourse", method = RequestMethod.GET)
    public Message addCourseAction(@Param("courseName") String courseName,
                                   @Param("courseNum") Integer  courseNum,
                                   @Param("academyName") String academyName,
                                   @Param("courseCredihour") Integer  courseCredihour,
                                   @Param("coursePeriod") Integer  coursePeriod,
                                   @Param("courseType") String courseType) {
        System.out.println("前台传来的数据："+"课程名："+courseName+"课程代码："+courseNum+"学院名称："+academyName+"学分："+courseCredihour+"学时："+coursePeriod+"类型："+courseType);
        Course course=new Course();
        System.out.println(course);
        course.setCourseName(courseName);
        course.setCourseNum(courseNum);
        Msg academy_id=academyService.queryacademy_idByacademy_name(academyName);
        course.setAcademyId(academy_id.getCode());
        course.setCourseCredithour(courseCredihour);
        course.setCoursePeriod(coursePeriod);
        course.setCourseType(courseType);
        Message flag = courseService.addCourse(course);//检查方法执行是否成功
        System.out.println(flag);
        return flag;
    }

    /**
     * @Description 课程删除Acion层
     * @author 20155878 鄢前州
     * @methodName deleteCourseAction
     * @returnType
     * @create 21:52 2018/5/13
     **/
    @RequestMapping(value = "/deleteCourse", method = RequestMethod.GET)
    public Message deleteCourseAction(@Param("courseName") String courseName) {
        System.out.println("前台传来的参数："+courseName);
        Integer courseId=courseService.querycourse_idBycourse_name(courseName);
        Message flag = courseService.deleteCourseBycourse_id(courseId);
        System.out.println(flag);
        return flag;
    }

    /**
     * @Description 课程查询Acion层, 连接Academy表查询课程
     * @author 20155878 鄢前州
     * @methodName queryAllcourseAction
     * @returnType Msg
     * @create 21:53 2018/5/13
     **/
    @RequestMapping(value = "/queryAllCourse", method = RequestMethod.GET)
    public Message queryCourseAction() {
        Message flag=courseService.queryAllCouse2();
        System.out.println(flag);
        return flag;
}

    /**
     * @Description
     * @author 20155878 鄢前州
     * @methodName
     * @returnType
     * @create 20:31 2018/5/23
     **/
    @RequestMapping(value = "/queryOneCourse", method = RequestMethod.GET)
    public Message queryOneCourseAction(@Param("courseName") String courseName) {
        int courseId = courseService.querycourse_idBycourse_name(courseName);
        Message flag = courseService.queryCouseBycourse_id(courseId);
        System.out.println(flag);
        return flag;
    }

    /**
     * @Description 课程更新Action
     * @author 20155878 鄢前州
     * @methodName 更新课程
     * @returnType
     * @create 21:54 2018/5/13
     **/
    @RequestMapping(value = "/updataCourse", method = RequestMethod.GET)
    public Message updataCourse(@Param("courseNameBefore") String courseNameBefore,
                                @Param("courseName") String courseName,
                                @Param("courseNum") Integer  courseNum,
                                @Param("academyName") String academyName,
                                @Param("courseCredihour") Integer  courseCredihour,
                                @Param("coursePeriod") Integer  coursePeriod,
                                @Param("courseType") String courseType)  {
        System.out.println("之前的课程名："+courseNameBefore);
        int courseId = courseService.querycourse_idBycourse_name(courseNameBefore);
        System.out.println(courseId);
        Course course = new Course();//新建一个课程实体封装数据
        course.setCourseId(courseId);
        course.setCourseName(courseName);
        course.setCourseNum(courseNum);
        course.setCourseCredithour(courseCredihour);
        course.setCoursePeriod(coursePeriod);
        course.setCourseType(courseType);
        Msg academyId = academyService.queryacademy_idByacademy_name(academyName);//获取学院id
        System.out.println(academyId.getCode());
        course.setAcademyId(academyId.getCode());//获取学院id
        if (academyId.getCode() == 0) {
            return Message.fail();
        } else {
            Message flag = courseService.updateCourseBycourse_id( course);
            return flag;
        }
    }
     /**
          * @Description
          * @author 20155878 鄢前州
          * @methodName 查询一个课程
          * @returnType
          * @create 15:13 2018/5/26
          **/
    @RequestMapping(value = "/queryAcourse", method = RequestMethod.GET)
    public Message queryAcourse(@Param("courseName") String courseName){
        Integer courseId=courseService.querycourse_idBycourse_name(courseName);
        Message flag=courseService.queryCouseBycourse_id(courseId);
        System.out.println(flag);
        return flag;
    }
}
